package cn.kys.modules.system.service.impl;

import cn.kys.common.utils.SecurityUtils;
import cn.kys.modules.security.security.vo.JwtUser;
import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.GetDeptIdByLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class GetDeptIdByLoginUserImpl implements GetDeptIdByLoginUser {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private ToolsResposity toolsResposity;


    @Override
    public Long getUserId() {
        JwtUser User = (JwtUser) userDetailsService.loadUserByUsername(SecurityUtils.getUsername());
        Long userId = User.getId();
        return userId;

    }

    @Override
    public String getDeptName() {

        JwtUser jwtUser = (JwtUser) userDetailsService.loadUserByUsername(SecurityUtils.getUsername());
        String dept = jwtUser.getDept();
        return dept;
    }

    @Override
    public Long getDeptId() {

        JwtUser jwtUser = (JwtUser) userDetailsService.loadUserByUsername(SecurityUtils.getUsername());
        String dept = jwtUser.getDept();
        Map map = toolsResposity.exeSql(String.format("select id from dept where name='%s'", dept));
        Long deptId = null;
        if (map != null) {
            deptId = (Long) map.get("id");
        }

        return deptId;
    }

    @Override
    public String getUserName() {

        JwtUser jwtUser = (JwtUser) userDetailsService.loadUserByUsername(SecurityUtils.getUsername());
        String username = jwtUser.getUsername();
        return username;
    }


}
